package unitTests;

import static org.junit.Assert.assertEquals;

import java.awt.Checkbox;
import java.awt.Color;
import java.util.List;

import javax.swing.JCheckBox;
import javax.swing.JPanel;

import mainpackage.DataManager;

import org.junit.Test;
import org.mockito.ArgumentMatcher;

import ui.MainPresenter;
import ui.MainWindow;

import db.DBCreator;

import static org.mockito.Mockito.*;

public class MainPresenterTest {

	MainWindow mockedMainWindow = mock(MainWindow.class);
	MainPresenter mainPresenter = new MainPresenter(mockedMainWindow);

	@Test
	public void testMock() throws Exception {
		// mock creation
		List mockedList = mock(List.class);

		// using mock object
		mockedList.add("one");
		mockedList.clear();

		// verification
		verify(mockedList).add("one");
		verify(mockedList).clear();
	}

	@Test
	public void viewChangeButtons() throws Exception {
		mainPresenter.monthViewButtonClicked();
		verify(mockedMainWindow).setWeekButtonSelected(false);
		verify(mockedMainWindow, times(1)).changeMainPanelContents(
				"MONTH_PANEL");

		mainPresenter.weekViewButtonClicked();
		verify(mockedMainWindow).setMonthButtonSelected(false);
		verify(mockedMainWindow, times(1))
				.changeMainPanelContents("WEEK_PANEL");

		mainPresenter.weekViewButtonClicked();
		verify(mockedMainWindow).setMonthButtonSelected(true);
		verify(mockedMainWindow, times(2)).changeMainPanelContents(
				"MONTH_PANEL");
		// verify(mockedMainWindow, times(1)).setMonthButtonSelected(false);

		mainPresenter.monthViewButtonClicked();
		verify(mockedMainWindow, times(2))
				.changeMainPanelContents("WEEK_PANEL");
	}

	@Test
	public void setDataManager() throws Exception {
		DBCreator.createDBWithDummyData("2_db1.db");
		mainPresenter.setDataManager(new DataManager("2_db1.db"));

		ArgumentMatcher<JPanel> checkBoxColorMatcherBlue = new ArgumentMatcher<JPanel>() {
			public boolean matches(Object panel) {
				JPanel jpanel = (JPanel)panel;
				JCheckBox j = (JCheckBox) jpanel.getComponent(1);
				Color c = j.getForeground();
				return c.getRGB() == Color.BLUE.getRGB();
			}
		};
		
		ArgumentMatcher<JPanel> checkBoxColorMatcherGreen = new ArgumentMatcher<JPanel>() {
			public boolean matches(Object panel) {
				JPanel jpanel = (JPanel)panel;
				JCheckBox j = (JCheckBox) jpanel.getComponent(1);
				Color c = j.getForeground();
		    	return c.getRGB() == (new Color(34, 177, 76)).getRGB();
		    }
		};

		verify(mockedMainWindow, times(1)).addCheckBoxPanel(argThat(checkBoxColorMatcherGreen));
		verify(mockedMainWindow, times(1)).addCheckBoxPanel(argThat(checkBoxColorMatcherBlue));
	}
}
